layui.use(['table', 'admin', 'ax','simTool'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var simtool = layui.simTool;
    var admin = layui.admin;

    /**
     * redis表管理
     */
    var redis = {
        tableId: "redisTable"
    };

    /**
     * 初始化表格的列
     */
    redis.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true,  title: 'ID'},
            {field: 'key', sort: true,align:'center', title: 'Redis Key'},
            {field: 'value', sort: true, align:'center', title: 'Redis Value'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minWidth: 180}
        ]];
    };

    /**
     * 点击查询按钮
     */
    redis.search = function () {
        var queryData = {};
        queryData['key'] = $("#key").val();
        table.reload(redis.tableId, {where: queryData
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };

    /**
     *
     */
    var sim = new simtool("","");
    sim.updateButton("/redis/editItem",redis.tableId,"status",[1,0])

    /**
     * 弹出添加对话框
     */
    redis.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加Redis',
            area:["40%","40%"],
            maxmin:'true',
            content: Feng.ctxPath + '/redis/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(redis.tableId);
            }
        });
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    redis.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改Redis',
            area:["50%","50%"],
            content: Feng.ctxPath + '/redis/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(redis.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    redis.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/redis/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(redis.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };



    // 渲染表格
    var tableResult = table.render({
        elem: '#' + redis.tableId,
        url: Feng.ctxPath + '/redis/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: redis.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        redis.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        redis.openAddDlg();
    });


    // 工具条点击事件
    table.on('tool(' + redis.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            redis.openEditDlg(data);
        } else if (layEvent === 'delete') {
            redis.onDeleteItem(data);
        }
    });


});
